package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> findAll() {
        return nursingLevelMapper.selectAll();
    }

    @Override
    public PageResponse findPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevelVo> vo = nursingLevelMapper.selectPage(name,status);
        for (NursingLevelVo nursingLevelVo : vo) {
            if(nursingLevelVo.getStatus() == 1){
                nursingLevelVo.setCount(1);
            }
        }
        return PageResponse.of(vo, NursingLevelVo.class);
    }

    @Override
    public void addNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtil.copyProperties(dto,nursingLevel);
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public NursingLevelVo findById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }

    @Override
    public void deleteLevel(Long id) {
        //删除 和 编辑 等级 时，等级不能是启用状态 status = 1
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        Integer status = nursingLevelVo.getStatus();
        nursingLevelMapper.deleteLevel(id);
    }

    @Override
    public void updateLevel(NursingLevelDto dto) {
        if(dto == null){
            throw new BaseException(BasicEnum.LEVEL_UPDATE_FAIL);
        }
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtil.copyProperties(dto,nursingLevel);
        Long id = nursingLevel.getId();
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        Integer status = nursingLevelVo.getStatus();
        if(status == 1){
            return;
        }
        nursingLevelMapper.update(nursingLevel);
    }
}
